package org.example.service.impl;
import org.example.config.SpringConfig;
import org.example.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TransactionRunner {
    private static final Logger log = LoggerFactory.getLogger(TransactionRunner.class);

    public static void main(String[] args) {
        // 1. 加载 Spring 配置（替代测试注解的作用）
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

        // 2. 获取 Service 实例（替代 @Autowired 注入）
        AccountService accountService = context.getBean(AccountService.class);

        // 3. 执行转账逻辑（和测试类相同）
        String out = "zs";
        String in = "ls";
        int money = 500;

        try {
            log.info("=== 开始转账：{} → {}，{}元 ===", out, in, money);
            accountService.transfer(out, in, money);
            log.info("=== 转账成功 ===");
        } catch (Exception e) {
            log.error("=== 转账失败：{} ===", e.getMessage());
            e.printStackTrace();
        } finally {
            // 关闭 Spring 容器
            context.close();
        }
    }
}